package com.baizhi.controller;

import com.baizhi.aop.CacheFind;
import com.baizhi.entity.Emp;
import com.baizhi.entity.EmpCondition;
import com.baizhi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisCluster;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("emp")
@CrossOrigin
@Slf4j
public class EmpController {


    @Autowired
    private EmpService empService;

    @Value("${photo.dir}")
    private String realPath;

    @Autowired
    private JedisCluster jedis;

    //修改员工信息
    @PostMapping("update")
    public Map<String, Object> update(Emp emp, MultipartFile photo) throws IOException {
        if(jedis.exists("findAll")){
            jedis.del("findAll");
            log.info("Redis中的键被删除-----------");
        }
        log.info("员工信息: [{}]", emp.toString());
        Map<String, Object> map = new HashMap<>();
        try {
            if(photo!=null&&photo.getSize()!=0){
                //获取文件名称的方法 photo.getOriginalFilename()
                log.info("头像信息: [{}]", photo.getOriginalFilename());
                //头像保存    FilenameUtils.getExtension(photo.getOriginalFilename()用来获取文件后缀
                String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath, newFileName));
                //设置头像地址
                emp.setPath(newFileName);
            }
            //保存员工信息
            empService.update(emp);
            map.put("state", true);
            map.put("msg", "员工信息保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "员工信息保存失败!");
        }
        return map;
    }

    //根据id查询员工信息实现
    @GetMapping("findOne")
    public Emp findOne(String id){
        log.info("查询员工信息的id: [{}]",id);
        return empService.findOne(id);
    }

    //删除员工信息实现
    @GetMapping("delete")
    public Map<String, Object> delete(String id) {
        if(jedis.exists("findAll")){
            jedis.del("findAll");
            log.info("Redis中的键被删除-----------");
        }
        log.info("删除员工的id:[{}]",id);
        Map<String, Object> map = new HashMap<>();
        try {
            //删除员工头像
            Emp emp = empService.findOne(id);
            String url=realPath+emp.getPath();
            //File file = new File(realPath, emp.getPath());
            File file = new File(url);
            if(file.exists())file.delete();//删除头像
            //删除员工信息
            empService.delete(id);
            map.put("state",true);
            map.put("msg","删除员工信息成功!");
        }catch (Exception e){
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除员工信息失败!");
        }
        return map;
    }


    //保存员工信息

    @PostMapping("save")
    public Map<String, Object> save(Emp emp, MultipartFile photo) throws IOException {
        if(jedis.exists("findAll")){
            jedis.del("findAll");
            log.info("Redis中的键被删除-----------");
        }
        log.info("员工信息: [{}]", emp.toString());
        log.info("头像信息: [{}]", photo.getOriginalFilename());
        Map<String, Object> map = new HashMap<>();
        try {
            //生存文件名称  getExtension()是获取文件后缀的方法  getOriginalFilename()获取文件名称的方法
            String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            //凭借文件全路径
            String url=realPath+newFileName;
            //上传文件
            photo.transferTo(new File(url)); //photo.transferTo(new File(realPath, newFileName));
            //将头像名称保存在数据库
            emp.setPath(newFileName);
            //保存员工信息
            empService.save(emp);
            map.put("state", true);
            map.put("msg", "员工信息保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "员工信息保存失败!");
        }
        return map;
    }


    /*@GetMapping("findAll")
    public Map<String,Object> findAll(Integer page,Integer rows) {
        System.out.println("页码值:"+page+"      行数:"+rows);
        Map<String, Object> map = empService.findAll(page, rows);
        List<Emp> emp= (List<Emp>)map.get("emps");
        System.out.println("总行数:"+map.get("rowCount"));
        System.out.println("总页数"+map.get("pageCount"));
        System.out.println("数据列表如下:");
        for (Emp e:
             emp) {
            System.out.println(e);
        }
        return map;
    }*/

    /**
     * 分页查询
     * @param page  http://localhost:8990/emsvue/emp/findAllPage?page=1&rows=10
     * @param rows  String name,Integer age,Double salary
     * @return
     */
    @GetMapping("findAllPage")
    public Map<String,Object> findAllPage(Integer page, Integer rows, String name,Integer minAge,Integer maxAge,Double minSalary,Double maxSalary) {
        log.info("姓名:"+name);
        log.info("最低薪资:"+minSalary+"    最高薪资:"+maxSalary);
        log.info("最低年龄:"+minAge+"    最高年龄:"+maxAge);
        log.info("当前页是=================="+page);
        Map<String, Object> map = empService.findAllPage(page, rows,name,minAge,maxAge,minSalary,maxSalary);

        List<Emp> li= (List<Emp>)map.get("emps");
        log.info("控制层的findAllPage方法的总行数:"+map.get("rowCount"));
        log.info("控制层的findAllPage方法的总页数:"+map.get("pageCount"));
        log.info("控制层数据列表如下:");

        return map;
    }


}
